package com.topteam.graph;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

/**
 * Date:Create by 2021/6/22
 * Describe: 用MPAndroidChart绘制饼图、柱状图，分别用ViewPager来展现
 * author: leo
 * Email: le112233@126.com
 */
public class MainActivity extends AppCompatActivity {

     private TabLayout tab_v;
     private ViewPager vp_v;
     private MyAdapter myAdapter;
     private List<Fragment> fragmentList = new ArrayList<>();
     private List<String> titles = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tab_v = findViewById(R.id.tab);
        vp_v = findViewById(R.id.vp);
        //把TabLayout 和 ViewPager进行关联
        tab_v.setupWithViewPager(vp_v);
        tab_v.setTabMode(TabLayout.MODE_SCROLLABLE);

        fragmentList.add(new Fragment_PieChart());
        fragmentList.add(new Fragment_BarChart());
        fragmentList.add(new Fragment_LineChart());
        fragmentList.add(new Fragment_Plato());
        titles.add("饼图");
        titles.add("柱形图");
        titles.add("折线图");
        titles.add("柏拉图");

        myAdapter = new MyAdapter(getSupportFragmentManager(),fragmentList,titles);
        vp_v.setAdapter(myAdapter);
        vp_v.setOffscreenPageLimit(4);

    }

    /**
     * ViewPager适配器
     */
    class MyAdapter extends FragmentPagerAdapter{

        private List<Fragment> dataList;
        private List<String> titles;

        public MyAdapter(FragmentManager fm,List<Fragment> dataList,List<String> titles) {
            super(fm);
            this.dataList = dataList;
            this.titles = titles;
        }

        @Override
        public Fragment getItem(int position) {
            return dataList.get(position);
        }

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }


}
